package com.nbgroup.service.Appraise;

import com.nbgroup.dao.appraise.AppraiseRepository;
import com.nbgroup.entity.Appraise;
import com.nbgroup.entity.Ichnography;
import com.nbgroup.utils.PageUtil;
import com.nbgroup.utils.QueryUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.management.Query;
import java.util.Date;
import java.util.List;

/**
 * Created by wyt on 2018/8/27.
 */

@Service
public class AppraiseService {
    @Autowired
    AppraiseRepository appraiseRepository;
    @Autowired
    private QueryUtil queryUtil;

    /*
     *   查询所有评价
     * @author 王玉涛
     * @Date 2018/8/27 16:31
     * @param
     * @return
     */
    public List<Appraise> getAppraises(Integer sts, Integer lid) {
        return appraiseRepository.findAllByStsAndLidOrderByCdateDesc(sts, lid);
    }

    /*
     *   查询所有评价
     * @author 王玉涛
     * @Date 2018/8/27 16:31
     * @param
     * @return
     */
    public Appraise saveAppraise(Appraise appraise) {
        if(appraise.getScore() == null ){
            appraise.setScore(5);
        }
        appraise.setCdate(new Date());
        Appraise appraise1 = appraiseRepository.saveAndFlush(appraise);
        return appraise1;
    }

    /**
     * 查询评价列表
     * @param
     * @author zzp
     * @date 2018/9/6 15:15
     */
    public PageUtil<Appraise> getAllAppraises(Integer page, Integer pageSize) {
        String sql = "SELECT\n" +
                "\ta.score score,\n" +
                "\ta.id id,\n" +
                "\ta.NAME NAME,\n" +
                "\ta.content content,\n" +
                "\ta.cdate cdate,\n" +
                "\ta.sts sts,\n" +
                "\ta.lid lid,\n" +
                "\tconcat( e.exhibition_name, '-', l.literature_title ) exhibitionName,\n" +
                "\ti.floor_id floorId \n" +
                "FROM\n" +
                "\tappraise a\n" +
                "\tLEFT JOIN literature l ON a.lid = l.literature_id\n" +
                "\tLEFT JOIN exhibition e ON l.exhibtion_id = e.exhibition_id\n" +
                "\tLEFT JOIN ichnography i ON e.ichnography_id = i.ichnography_id \n" +
                "\twhere a.statue='0' \n" +
                "GROUP BY\n" +
                "\ta.cdate DESC";
        PageUtil<Appraise> appraisePageUtil = queryUtil.queryByPage(sql, Appraise.class, page, pageSize);
        return appraisePageUtil;
    }

    /**
     * 修改评价是否通过审核
     * @param
     * @return a
     * @author zzp
     * @date 2018/9/6 16:02
     */
    public String changeSts(Integer id, Integer sts) {
        String sql = "SELECT\n" +
                "\t*\n" +
                "FROM\n" +
                "\tappraise where id = '" + id + "'";
        List<Appraise> appraises = queryUtil.query(sql, Appraise.class);
        if (sts == 0) {
            Appraise appraise = appraises.get(0);
            appraise.setSts(0);
            appraiseRepository.saveAndFlush(appraise);
            return "0";
        }else{
            appraises.get(0).setSts(1);
            appraiseRepository.saveAndFlush(appraises.get(0));
            return "1";
        }
    }

    /**
     * 统计分析 游客评论展示
     * 2018年9月10日, PM 03:49:36
     *
     * @author 姜文
     */
    public List<Appraise> getAppraiseForScreen(){
        String sql = "SELECT * FROM `appraise` WHERE sts = '0'and statue ='0' ORDER BY cdate DESC LIMIT 7";
        List<Appraise> appraiseList = queryUtil.query(sql, Appraise.class);
        for(Appraise appraise:appraiseList){
            if(appraise.getName().length() == 0){
                appraise.setName("匿名");
            }
            if(appraise.getContent().length() > 25){
                appraise.setContent(appraise.getContent().substring(0,22) + "......");
            }
            String str = "";
            for(int i=0;i<appraise.getScore();i++){
                str += "⭐";
            }
            appraise.setStar(str);
        }
        return appraiseList;
    }
    public List<Appraise> getAppraiseForScreenNew(){
        String sql = "SELECT * FROM `appraise` WHERE sts = '0'and statue ='0' ORDER BY cdate DESC LIMIT 7";
        List<Appraise> appraiseList = queryUtil.query(sql, Appraise.class);
        for(Appraise appraise:appraiseList){
            if(appraise.getName().length() == 0){
                appraise.setName("匿名");
            }else if(appraise.getName().length()>6){
                appraise.setName(appraise.getName().substring(0,6));
            }
            if(appraise.getContent().length() > 25){
                appraise.setContent(appraise.getContent().substring(0,22) + "......");
            }
            String str = "";
            for(int i=0;i<appraise.getScore();i++){
                str += "⭐";
            }
            appraise.setStar(str);
        }
        return appraiseList;
    }
    public List<Appraise> getAppraiseForScreenNewByTime(String start ,String end){
        String sql = "SELECT * FROM `appraise` WHERE sts = '0'and statue ='0' AND cdate > '"+start+"' AND cdate < '"+end+"'  ORDER BY cdate DESC LIMIT 7";
        List<Appraise> appraiseList = queryUtil.query(sql, Appraise.class);
        for(Appraise appraise:appraiseList){
            if(appraise.getName().length() == 0){
                appraise.setName("匿名");
            }else if(appraise.getName().length()>6){
                appraise.setName(appraise.getName().substring(0,6));
            }
            if(appraise.getContent().length() > 25){
                appraise.setContent(appraise.getContent().substring(0,22) + "......");
            }
            String str = "";
            for(int i=0;i<appraise.getScore();i++){
                str += "⭐";
            }
            appraise.setStar(str);
        }
        return appraiseList;
    }

    public Appraise findOne(Integer lid) {
        return appraiseRepository.findOne(lid);
    }
    public void del(Integer lid) {
        Appraise one = appraiseRepository.findOne(lid);
        one.setStatue(1);
        appraiseRepository.saveAndFlush(one);
    }
}
